package com.ricky04;

import com.ricky04.ApplicationContext.MyApplicationContextInitializer;
import com.ricky04.Listener.BookServletContextListener;
import com.ricky04.event.MyApplicationEvent;
import com.ricky04.filter.BookFilter;
import com.ricky04.jdbc.ChannelDao;
import com.ricky04.jdbc.dao.Channel2Dao;
import com.ricky04.log.LogPrinterDao;
import com.ricky04.servlet.BookServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 事件监听步骤：
 *  1.定义事件
 *  2.定义监听
 *  3.发布事件
 */
//不使用SpringBoot提供的错误页面  方便进行调试
@EnableAutoConfiguration(exclude = ErrorMvcAutoConfiguration.class)
//@EnableAutoConfiguration
@ComponentScan
@ServletComponentScan
@EnableTransactionManagement
//@EnableWebMvc
@Controller
//@EnableZDRedis
@PropertySource("classpath:/my.properties")
public class SpringBootStart05 {
    @Value("${test}")
    private String test;
    /**
     * 注入servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean createBookServlet(){
        System.out.println("test:---"+test);
        ServletRegistrationBean servlet=new ServletRegistrationBean(new BookServlet(),"/book.do");
        return servlet;
    }

    /**
     * 注入Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean createBookFilter(){
        FilterRegistrationBean filter=new FilterRegistrationBean();
        filter.setFilter(new BookFilter());
        filter.setUrlPatterns(Arrays.asList("/book.do"));
        return filter;
    }
    /**
     * 注入Listener
     * @return
     */
    @Bean
    public ServletListenerRegistrationBean<BookServletContextListener> createBookServletListenerRegistrationBean(){
        ServletListenerRegistrationBean<BookServletContextListener> listener=new ServletListenerRegistrationBean(new BookServletContextListener());
        return listener;
    }

    @RequestMapping("/")
    public String index(ModelMap map){
        map.put("username","老白");
        System.out.println("首页");
        return "index";
    }
    @RequestMapping(value = "/login")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("login2");

        List<String> userList=new ArrayList<String>();
        userList.add("admin");
        userList.add("user1");
        userList.add("user2");

        modelAndView.addObject("userList", userList);
        return modelAndView;
    }
    @GetMapping("/index2")
    @ResponseBody
    public String index2(){
        try {
            System.out.println("首页");
            throw new IllegalArgumentException("五百错误");
        }catch (Exception e){
            throw new IllegalArgumentException("五百错误");
        }

    }
    public static void main2(String[] args) {
//        SpringApplication.run(SpringBootStart05.class,args);
        SpringApplication app=new SpringApplication(SpringBootStart05.class);
        app.addInitializers(new MyApplicationContextInitializer());
        //可通过spring.banner.location=my.txt来指定文件路径
        //如果classpath下有banner.txt文件的话 会自动读取文件中的
//        app.setBannerMode(Banner.Mode.OFF);//关闭头部SpringBanner图
        //注册监听  或者listener中使用@component方式
        // DelegatingApplicationListener 或者在application.properties中配置context.listener.classes=com.ricky04.event.MyApplicationListener
        //EventListenerMethodProcessor.processBean或者使用@eventListener

//        app.addListeners(new MyApplicationListener());


//        ConfigurableApplicationContext context = app.run("aa","bb","cc");
        /**
         * 运行参数设置
         * --ricky=rickyvalue --ricky01=rickyvalue01
         */
        ConfigurableApplicationContext context = app.run(args);
//        System.out.println(context.getBean("createRunnableByCore"));
        context.publishEvent(new MyApplicationEvent(new Object()));//发布事件

        context.getBean(Runnable.class).run();
        ApplicationArguments args2 = context.getBean(ApplicationArguments.class);
        System.out.println(args2.getOptionNames());
        System.out.println(args2.getOptionValues(args2.getOptionNames().iterator().next()));
        context.getBean(ChannelDao.class).AddChannel("理财","licai");
        context.getBean(Channel2Dao.class).AddChannel("理财","licai");
        DataSource ds = context.getBean(DataSource.class);
        System.out.println(ds.getClass());

        Jedis jedis = context.getBean(Jedis.class);
        jedis.set("name","ricky");
        System.out.println("redis："+jedis.get("name"));
//        context.close();
    }



    public static void main(String[] args) {
        SpringApplication app=new SpringApplication(SpringBootStart05.class);
        ConfigurableApplicationContext context = app.run(args);
        context.getBean(LogPrinterDao.class).log();

//        context.close();
    }
}
